package com.watf.functions.core;


/*
 * This Class file is mainly used to get the variable from the properties files. 
 * 
 * Please update it if you think the function used here is not very perfect.  
 * 
 * History:
 * 2011-6-28  First Draft   Jester
 */
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.testng.Reporter;

public abstract class DefaultConfigUtil
{
	
    private static final ResourceBundle WatfData_BUNDLE = ResourceBundle
    .getBundle("com.watf.testdata.WatfData");
	
	
	
    private static final ResourceBundle ConfigData_BUNDLE = ResourceBundle
            .getBundle(getWatfData("ConfigDataLocation"));
    
    private static final ResourceBundle CommonData_BUNDLE = ResourceBundle
    .getBundle(getWatfData("CommonDataLocation"));
    
    
    /**
     * Get the Configure variables from the WatfData.properties file.
     */
    public static String getWatfData(String key)
    {
        try
        {
            return WatfData_BUNDLE.getString(key);
        }
        catch (MissingResourceException e)
        {
            Reporter.log("Failed to Get the " + key + " from the .properties file!");
            return null;
        }
    }
    
    /**
     * Get the Configure variables from the CommonData.properties file.
     */
    public static String getConfigData(String key)
    {
        try
        {
            return ConfigData_BUNDLE.getString(key);
        }
        catch (MissingResourceException e)
        {
            Reporter.log("Failed to Get the " + key + " from the .properties file!");
            return null;
        }
    }

    /**
     * Get the Common variables from the CommonData.properties file.
     */
    public static String getCommonData(String key)
    {
        try
        {
            return CommonData_BUNDLE.getString(key);
        }
        catch (MissingResourceException e)
        {
            Reporter.log("Failed to Get the " + key + " from the .properties file!");
            return null;
        }
    }
    /**
     * Get the variables from the specific the .properties file.
     */
    public static String getDataInCase(String testCaseName, String key)
    {
        try
        {
            ResourceBundle vari_Bundle = ResourceBundle
                    .getBundle(testCaseName.replace("testcases", "testdata"));
            return vari_Bundle.getString(key);
        }
        catch (MissingResourceException e)
        {
            Reporter.log("Failed to Get the " + key + " from the " + testCaseName
                    + ".properties file!");
            return null;
        }
        catch (Exception e) 
        {
            Reporter.log(e.getMessage());
            return null;
        }
    }

    /**
     * Get the variables from the specific the .properties file which need the
     * Path variable.
     */
    public static String getPath(String testCaseName, String key)
    {
        try
        {
            ResourceBundle TestData_BUNDLE2 = ResourceBundle
                    .getBundle(testCaseName.replace("testcases", "testdata"));
            return ConfigData_BUNDLE.getString("Base_Path")
                    + TestData_BUNDLE2.getString(key);
        }
        catch (MissingResourceException e)
        {
            Reporter.log("Failed to Get the " + key + " from the " + testCaseName
                    + ".properties file!");
            return null;
        }
        catch (Exception e)
        {
            Reporter.log(e.getMessage());
            return null;
        }
    }
}
